package work.atpgroup.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import work.atpgroup.entity.User;
import work.atpgroup.entity.WebPage;
import work.atpgroup.entity.tool.ApplicationProperties;
import work.atpgroup.service.PermissionService;
import work.atpgroup.service.ResourceService;
import work.atpgroup.service.WebPageService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

@Controller
public class WebpageController {
    @Resource(name="resourceService")
    private ResourceService resourceService;
    @Resource(name="webPageService")
    private WebPageService webPageService;
    @Resource(name="permissionService")
    private PermissionService permissionService;
    /**私有地址**/
    @GetMapping("/webPage_bed/webpage/{preferPath}")
    public String webpageDirector(@PathVariable String preferPath, Model model, HttpServletRequest request){
        return webpageGetter(preferPath, model);
    }
    @GetMapping("/public/**/{preferPath}")
    public String webpageDirector2(@PathVariable String preferPath, Model model, HttpServletRequest request){
        return webpageGetter(preferPath, model);
    }

    private String webpageGetter(@PathVariable String preferPath, Model model) {
        WebPage webPage = webPageService.getWebPageByUri(preferPath);
        if(webPage==null){
            model.addAttribute("webPage_errors","你要访问的地址不存在");
            return "public/error/webPageError";
        }
        if((!webPage.getStatus())&&permissionService.getPermissionById(ApplicationProperties.permissionId).getWebpagePermission()){
            model.addAttribute("webPage_errors","该页面还没有被审核，暂时无法查看");
            return "public/error/webPageError";
        }
        model.addAttribute("webPage",webPage);
        model.addAttribute("privacy",false);
        webPage.setPublicCssPath(ApplicationProperties.getPublicUriPath(resourceService.getResourceLinkPathById(webPage.getCssId())));
        webPage.setPublicJsPath(ApplicationProperties.getPublicUriPath(resourceService.getResourceLinkPathById(webPage.getJsId())));
        return "public/webPage";
    }

    @GetMapping("/webpage/{preferPath}")
    public String webpageBedDirector(@PathVariable String preferPath, Model model, HttpServletRequest request){
        WebPage webPage = webPageService.getWebPageByUri(preferPath);
        if(webPage==null){
            model.addAttribute("webPage_errors","你要访问的地址不存在");
            return "public/error/webPageError";
        }
        User user = (User) request.getSession().getAttribute("user");
        if(user==null){
            model.addAttribute("webPage_errors","你还没有登录，而这个地址是一个私有地址，需要登录后访问");
            return "public/error/webPageError";
        }
        if(!Objects.equals(user.getUserId(), webPage.getUserId())){
            model.addAttribute("webPage_errors","登录信息不符合所有人，你没有权限查看本页面");
            return "public/error/webPageError";
        }
        model.addAttribute("webPage",webPage);
        model.addAttribute("privacy",true);
        return "public/webPage";
    }
    @GetMapping("/createMyWebPage")
    public String createMyWebPage(){
        return "public/createMyWebPage";
    }
}
